import { Module } from '@nestjs/common';
import { UploadController } from './upload.controller';
import { UploadService } from './upload.service';
import { MulterModule } from '@nestjs/platform-express';
import { diskStorage } from 'multer';
import * as path from 'path';

@Module({
 imports: [
  MulterModule.register({
   storage: diskStorage({
    // 指定文件存储目录
    destination: path.join(__dirname, '../uploads'),
    // 通过时间戳来重命名上传的文件名
    filename: (_, file, callback) => {
         const name = Buffer.from(file.originalname, "latin1").toString("utf8");
        const timestamp = new Date().toISOString().replace(/[-T:.Z]/g, '');
        const baseName = path.basename(name,path.extname(name))
        const extName = path.extname(name)
         const fileName = `${
            baseName+"_"+timestamp +extName
         }`;
         return callback(null, fileName);
         },
    }),
   }),
  ],
 controllers: [UploadController],
 providers: [UploadService],
})
export class UploadModule {}

